package com.helloae.chap05;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IEnumRelationshipClass;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IObjectClass;
import com.esri.arcgis.geodatabase.IObjectClassProxy;
import com.esri.arcgis.geodatabase.IRelationshipClass;
import com.esri.arcgis.geodatabase.ITable;
import com.esri.arcgis.geodatabase.MemoryRelationshipClassFactory;
import com.esri.arcgis.geodatabase.esriRelCardinality;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 将一个shp与三个dbf文件relate关联，具体情况如下：<br>
 * mosoils.shp与comp.dbf通过MUSYM关联，comp.dbf与forest.dbf通过MUID关联，forest.dbf与plantnm.dbf通过PLANTSYM关联。<br>
 * 示例章节：5.6.4
 * @author zhushijie
 *
 */
public class RelationalDatabase {

	public static void main(String[] args) throws UnknownHostException, IOException {
		// 初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		//使用ShapefileWorkspaceFactory.openFromFile的方法创建一个Workspace
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap05", 0);
		//加载图层wp.shp
		IFeatureClass pFeatureClass = pFeatureWorkspace
				.openFeatureClass("mosoils");
		
		//打开用于relate的数据集comp，即表comp.dbf
		ITable pCompTable = pFeatureWorkspace.openTable("comp");
		IObjectClass pCompClass =new IObjectClassProxy(pCompTable);
		//打开用于relate的数据集forest，即表forest.dbf
		ITable pForestTable = pFeatureWorkspace.openTable("forest");
		IObjectClass pForestClass =new IObjectClassProxy(pForestTable);
		//打开用于relate的数据集plantnm，即表plantnm.dbf
		ITable pPlantnmTable = pFeatureWorkspace.openTable("plantnm");
		IObjectClass pPlantnmClass =new IObjectClassProxy(pPlantnmTable);
		
		MemoryRelationshipClassFactory pMemRelFact = new MemoryRelationshipClassFactory();
		IRelationshipClass pRelClass1 = pMemRelFact.open("mosoils-comp", pCompClass, "MUSYM", pFeatureClass, "MUSYM", "mosoils", "comp", esriRelCardinality.esriRelCardinalityManyToMany);
		IRelationshipClass pRelClass2 = pMemRelFact.open("comp-forest", pForestClass, "MUID", pCompClass, "MUID", "comp", "forest", esriRelCardinality.esriRelCardinalityManyToMany);
		IRelationshipClass pRelClass3 = pMemRelFact.open("forest-plantnm", pPlantnmClass, "PLANTSYM", pForestClass, "PLANTSYM", "forest", "plantnm", esriRelCardinality.esriRelCardinalityManyToMany);
		
		FeatureLayer pFeatureLayer = new FeatureLayer();
		pFeatureLayer.setFeatureClassByRef(pFeatureClass);
		pFeatureLayer.addRelationshipClass(pRelClass1);
		pFeatureLayer.addRelationshipClass(pRelClass2);
		pFeatureLayer.addRelationshipClass(pRelClass3);

		//验证relate是否成功
		IEnumRelationshipClass pEnumRelClass = pFeatureLayer.getRelationshipClasses();
		IRelationshipClass pRelClass = pEnumRelClass.next();
		int i = 1;
		while(pRelClass!= null){
			System.out.println("第" + i + "次关联的两个要素名称为：");
			System.out.println("OriginClass.Name = " + pRelClass.getOriginClass().getAliasName());
			System.out.println("DestinationClass.Name = " + pRelClass.getDestinationClass().getAliasName());
			System.out.println();
			pRelClass = pEnumRelClass.next();
			i++;
		}
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
